import re
import pickle
#import Constants
import os

''' This file will be the basic configuration file for our purpose...
You can edit the values from here instead of the web interface if you have some errs'''

def getConstInfo(mode):
    '''If mode=5 The contents if mode=1 the declarations
     That one will get the contents of Constants.py'''

    #file_name="".join([os.getcwdu(),"/Constants.py"])
    #print file_name
    file_name="/home/makkalot/zope2.9/Products/baska/Constants.py"
    try:

        f=open(file_name,'r')
        print "Actik"

        Cnst=f.readlines()
        pattern='(.+?)(=)(.*)("|\')(.*)("|\')'#Construct structure
        cList=[]
        for word in Cnst:
            if word:
                result=re.match(pattern,word)
                if result :#if it is None
                    #print result.group(1).strip()," :",result.group(5).strip()
                    cList.append(result.group(mode).strip())
        return cList

                #print word

        f.close()

    except IOError:
        #print "Some error while opening the constant file"
        return 1#To show that some error occured

def resetConstInfo(confVal):
    """ That method will set the values that are taken from web gui"""
    #Take the default declration list
    #fname="".join([os.getcwdu(),"/Constants.py"])
    fname="/home/makkalot/zope2.9/Products/baska/Constants.py"
    dcList=getConstInfo(1)
    finalList=[]
    i=0
    for dec in dcList:

        finalList.append("".join([dec,"=",'\"%s\"'%(confVal[i]),'\n']))
        i+=1

    #print finalList
    #Write them to the file...
    try:
        print finalList
        print "Acmadan Once"
        f=open(fname,'w')
        print "Actik for write"
        f.writelines(finalList)
        f.close()

        return 0 #succes
    except IOError,e:
        print "Some error while opening the constant file :",e
        return 1#To show that some error occured

def setlogInfo(lineNum,fsize):
    """ That one sets some variables that are important for logging the first one is :tail
    and second one is filesize()"""
    try:
        main_path=os.getcwdu()
        theF=open("".join([main_path,"/","loginfo.txt"]),'w')#opening into the write mode
        todump=[lineNum,fsize]
        pickle.dump(todump,theF)
        theF.close()
        return 0
    
    except IOError:
        return 1

def getlogInfo():
    """ Gets back information from the tuple"""
    try:
        main_path=os.getcwdu()
        theF=open("".join([main_path,"/","loginfo.txt"]),'r')#opening into the write mode
        toload=[]
        toload=pickle.load(theF)
        theF.close()
        return toload
    
    except IOError:
        return 1


#The testing of the module :
if __name__=="__main__":
    #print setlogInfo(11,300)
    #print getlogInfo()
    print resetConstInfo(['newlhost', 'iptables', 'root', '010684', '/sbin/iptables',"/var/log/ulog/syslogemu.log1",'2'])
    print getConstInfo(5)
    